/* ===================

AX2E (AXESCON XACML 2.0 Engine) is the Java authorization engine, which implements OASIS XACML 2.0 standard.
Copyright (C) 2007 AXESCON LLC

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA


Contact: AXESCON LLC - info{at}axescon.com

 =================== */
package axescon.xacml.engine.fn;

import axescon.xacml.model.policy.AttributeValue;
import axescon.xacml.model.shared.attr.BooleanAttribute;
import axescon.xacml.api.*;

import java.lang.reflect.Array;

/**
 * @author Date: Sep 28, 2005
 *         Responsibilities: implements Bag functions from
 *         see axescon.xacml 2.0 spec ch. A.3.10 Bag functions
 */
public class FnBagBaseIsIn extends  AbstractFn implements Fn, BagFn {

    boolean execOnBag1(AttributeValue arg0, AttributeValue[] arg1) {
        for (int i = 0;arg1!=null&& i < arg1.length; i++) {
            AttributeValue av = arg1[i];
            if (arg0.equals(av)) return true;
        }
        return false;
    }

    public Class getReturnDataType() {
        return super.getBooleanReturnDataType();
    }

    public Liquid exec(Liquid[] args) throws ExpressionEvalIndeterminateException {
        return new Liquid(new BooleanAttribute(execOnBag1(args[0].getSingular(), args[1].getBag())));
    }

    public boolean execOnBag(AttributeValue arg0, AttributeValue[] arg1) {
        return execOnBag1(arg0, arg1);
    }

    public int getArgTypeListLength() {
        return 2;
    }

    public void setDataTypes(Class[] dataTypes) throws XacmlException {
        Class array = Array.newInstance(dataTypes[0],0).getClass();
        super.setDataTypes( new Class[]{dataTypes[0],array});
    }


}
